using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;

namespace Elementum
{
    public class Button : Image
    {
        public Animation _button;
        protected Animation _buttonPressRing;
        protected Animation _sprite;

        bool _enable = true;
        float _rotation = 5.5f * MathHelper.Pi / 180;
        float _alphaCount;
        Stage _stage;


        public Button(Stage stage, Animation button)
        {
            _baseAlpha = 0.5f;
            string buttonPath = "images/system/button";
            _buttonPressRing = new Animation(buttonPath,
                new Rectangle(2, 308, 100, 100), new Point(100, 100));
            _button = button;
            CurrentAnimation = _button;
            Scale = 1;
            Alpha = _baseAlpha;
            this.IsTouchChangeEvent += TouchChanged;
            _stage = stage;
        }

        public Animation ButtonSprite
        {
            get { return _sprite; }
            set
            {
                _sprite = value;
                if (_sprite == null)
                {//reset
                    if (Enable)
                        CurrentAnimation = _button;
                    else
                        CurrentAnimation = null;
                }

                else
                    CurrentAnimation = _sprite;
            }
        }

        public bool Enable
        {
            get { return _enable; }
            set { _enable = value;
            }
        }


        public void TouchChanged(bool touch)
        {
            if (!touch)
            {
                AlphaModifier a = new AlphaModifier(Alpha, _baseAlpha, 0, 18);
                ModifierContain m = new ModifierContain(this);
                a.ModifyDoneAction += a.UnRegister;
                m.AddModifier(a);
                this.ModifierContainer = m;
            }
            else
            {
                AlphaModifier a = new AlphaModifier(_baseAlpha, 0.89f, 0, 18);
                ModifierContain m = new ModifierContain(this);
                a.ModifyDoneAction += a.UnRegister;
                m.AddModifier(a);
                this.ModifierContainer = m;
            }
        }

        public override void OnDraw(SpriteBatch spriteBatch)
        {

            if (IsTouched&&Enable)
            {
                _buttonPressRing.OnDraw(spriteBatch, Position);
            }
            //else
            //    Alpha = _baseAlpha;
            base.OnDraw(spriteBatch);
            //_button.OnDraw(spriteBatch, Position);
        }

        // public override 

        public override void Update(int delta)
        {

            base.Update(delta);

            if (CurrentAnimation!=null)
            CurrentAnimation.LayerDepth = 0;
            if (IsTouched)
                _buttonPressRing.Rotation += _rotation;


            float alpha = (float)(Math.Sin(_alphaCount) + 1) / 2;
            _buttonPressRing.Alpha = alpha + 0.5f;
            _alphaCount += (float)(5.8 * MathHelper.Pi / 180);
            //if(_buttonPressAlpha>1)

        }

    }
}
